home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 198_02 / random.c < prev    next >
C/C++ Source or Header  |  1990-01-21  |  30KB  |  1,103 lines

  1. /*
  2.  * This file contains the command processing functions for a number of random
  3.  * commands. There is no functional grouping here, for sure.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. /*
  11.  * Set fill column to n.
  12.  */
  13. setfillcol(f, n)
  14. {
  15.         fillcol = n;
  16.     mlwrite("[Fill column is %d]",n);
  17.         return(TRUE);
  18. }
  19.  
  20. /*
  21.  * Display the current position of the cursor, in origin 1 X-Y coordinates,
  22.  * the character that is under the cursor (in hex), and the fraction of the
  23.  * text that is before the cursor. The displayed column is not the current
  24.  * column, but the column that would be used on an infinite width display.
  25.  * Normally this is bound to "C-X =".
  26.  */
  27. showcpos(f, n)
  28. {
  29.         register LINE   *lp;        /* current line */
  30.         register long   numchars;    /* # of chars in file */
  31.         register int    numlines;    /* # of lines in file */
  32.         register long   predchars;    /* # chars preceding point */
  33.         register int    predlines;    /* # lines preceding point */
  34.         register int    curchar;    /* character under cursor */
  35.         int ratio;
  36.         int col;
  37.     int savepos;            /* temp save for current offset */
  38.     int ecol;            /* column pos/end of current line */
  39.  
  40.     /* starting at the beginning of the buffer */
  41.         lp = lforw(curbp->b_linep);
  42.  
  43.     /* start counting chars and lines */
  44.         numchars = 0;
  45.         numlines = 0;
  46.         while (lp != curbp->b_linep) {
  47.         /* if we are on the current line, record it */
  48.         if (lp == curwp->w_dotp) {
  49.             predlines = numlines;
  50.             predchars = numchars + curwp->w_doto;
  51.             if ((curwp->w_doto) == llength(lp))
  52.                 curchar = '\n';
  53.             else
  54.                 curchar = lgetc(lp, curwp->w_doto);
  55.         }
  56.         /* on to the next line */
  57.         ++numlines;
  58.         numchars += llength(lp) + 1;
  59.         lp = lforw(lp);
  60.         }
  61.  
  62.     /* if at end of file, record it */
  63.     if (curwp->w_dotp == curbp->b_linep) {
  64.         predlines = numlines;
  65.         predchars = numchars;
  66.         curchar = '\n';
  67.     }
  68.  
  69.     /* Get real column and end-of-line column. */
  70.     col = getccol(FALSE);
  71.     savepos = curwp->w_doto;
  72.     curwp->w_doto = llength(curwp->w_dotp);
  73.     ecol = getccol(FALSE);
  74.     curwp->w_doto = savepos;
  75.  
  76.         ratio = 0;              /* Ratio before dot. */
  77.         if (numchars != 0)
  78.                 ratio = (100L*predchars) / numchars;
  79.  
  80.     /* summarize and report the info */
  81.     mlwrite("Line %d/%d Col %d/%d Char %D/%D (%d%%) char = 0x%x (%d)",
  82.         predlines+1, numlines+1, col, ecol,
  83.         predchars, numchars, ratio, curchar, curchar);
  84.         return (TRUE);
  85. }
  86.  
  87. getcline()    /* get the current line number */
  88.  
  89. {
  90.         register LINE   *lp;        /* current line */
  91.         register int    numlines;    /* # of lines before point */
  92.  
  93.     /* starting at the beginning of the buffer */
  94.         lp = lforw(curbp->b_linep);
  95.  
  96.     /* start counting lines */
  97.         numlines = 0;
  98.         while (lp != curbp->b_linep) {
  99.         /* if we are on the current line, record it */
  100.         if (lp == curwp->w_dotp)
  101.             break;
  102.         ++numlines;
  103.         lp = lforw(lp);
  104.         }
  105.  
  106.     /* and return the resulting count */
  107.     return(numlines + 1);
  108. }
  109.  
  110. /*
  111.  * Return current column.  Stop at first non-blank given TRUE argument.
  112.  */
  113. getccol(bflg)
  114. int bflg;
  115. {
  116.         register int c, i, col;
  117.         col = 0;
  118.         for (i=0; i<curwp->w_doto; ++i) {
  119.                 c = lgetc(curwp->w_dotp, i);
  120.                 if (c!=' ' && c!='\t' && bflg)
  121.                         break;
  122.                 if (c == '\t')
  123.                         col += tabsize - (col % tabsize) - 1;
  124.                 else if ((c&0x7F) < 0x20 || (c&0x7F) == 0x7F)
  125.                         ++col;
  126.                 ++col;
  127.         }
  128.         return(col);
  129. }
  130.  
  131. /*
  132.  * Set current column.
  133.  */
  134. setccol(pos)
  135.  
  136. int pos;    /* position to set cursor */
  137.  
  138. {
  139.         register int c;        /* character being scanned */
  140.     register int i;        /* index into current line */
  141.     register int col;    /* current cursor column   */
  142.     register int llen;    /* length of line in bytes */
  143.  
  144.     col = 0;
  145.     llen = llength(curwp->w_dotp);
  146.  
  147.     /* scan the line until we are at or past the target column */
  148.     for (i = 0; i < llen; ++i) {
  149.         /* upon reaching the target, drop out */
  150.         if (col >= pos)
  151.             break;
  152.  
  153.         /* advance one character */
  154.                 c = lgetc(curwp->w_dotp, i);
  155.                 if (c == '\t')
  156.                         col += tabsize - (col % tabsize) - 1;
  157.                 else if ((c&0x7F) < 0x20 || (c&0x7F) == 0x7F)
  158.                         ++col;
  159.                 ++col;
  160.         }
  161.  
  162.     /* set us at the new position */
  163.     curwp->w_doto = i;
  164.  
  165.     /* and tell weather we made it */
  166.     return(col >= pos);
  167. }
  168.  
  169. /*
  170.  * Twiddle the two characters on either side of dot. If dot is at the end of
  171.  * the line twiddle the two characters before it. Return with an error if dot
  172.  * is at the beginning of line; it seems to be a bit pointless to make this
  173.  * work. This fixes up a very common typo with a single stroke. Normally bound
  174.  * to "C-T". This always works within a line, so "WFEDIT" is good enough.
  175.  */
  176. twiddle(f, n)
  177. {
  178.         register LINE   *dotp;
  179.         register int    doto;
  180.         register int    cl;
  181.         register int    cr;
  182.  
  183.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  184.         return(rdonly());    /* we are in read only mode    */
  185.         dotp = curwp->w_dotp;
  186.         doto = curwp->w_doto;
  187.         if (doto==llength(dotp) && --doto<0)
  188.                 return (FALSE);
  189.         cr = lgetc(dotp, doto);
  190.         if (--doto < 0)
  191.                 return (FALSE);
  192.         cl = lgetc(dotp, doto);
  193.         lputc(dotp, doto+0, cr);
  194.         lputc(dotp, doto+1, cl);
  195.         lchange(WFEDIT);
  196.         return (TRUE);
  197. }
  198.  
  199. /*
  200.  * Quote the next character, and insert it into the buffer. All the characters
  201.  * are taken literally, with the exception of the newline, which always has
  202.  * its line splitting meaning. The character is always read, even if it is
  203.  * inserted 0 times, for regularity. Bound to "C-Q"
  204.  */
  205. quote(f, n)
  206. {
  207.         register int    s;
  208.         register int    c;
  209.  
  210.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  211.         return(rdonly());    /* we are in read only mode    */
  212.         c = tgetc();
  213.         if (n < 0)
  214.                 return (FALSE);
  215.         if (n == 0)
  216.                 return (TRUE);
  217.         if (c == '\n') {
  218.                 do {
  219.                         s = lnewline();
  220.                 } while (s==TRUE && --n);
  221.                 return (s);
  222.         }
  223.         return (linsert(n, c));
  224. }
  225.  
  226. /*
  227.  * Set tab size if given non-default argument (n <> 1).  Otherwise, insert a
  228.  * tab into file.  If given argument, n, of zero, change to true tabs.
  229.  * If n > 1, simulate tab stop every n-characters using spaces. This has to be
  230.  * done in this slightly funny way because the tab (in ASCII) has been turned
  231.  * into "C-I" (in 10 bit code) already. Bound to "C-I".
  232.  */
  233. tab(f, n)
  234. {
  235.         if (n < 0)
  236.                 return (FALSE);
  237.         if (n == 0 || n > 1) {
  238.                 stabsize = n;
  239.                 return(TRUE);
  240.         }
  241.         if (! stabsize)
  242.                 return(linsert(1, '\t'));
  243.         return(linsert(stabsize - (getccol(FALSE) % stabsize), ' '));
  244. }
  245.  
  246. #if    AEDIT
  247. detab(f, n)        /* change tabs to spaces */
  248.  
  249. int f,n;    /* default flag and numeric repeat count */
  250.  
  251. {
  252.     register int inc;    /* increment to next line [sgn(n)] */
  253.  
  254.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  255.         return(rdonly());    /* we are in read only mode    */
  256.  
  257.     if (f == FALSE)
  258.         n = 1;
  259.  
  260.     /* loop thru detabbing n lines */
  261.     inc = ((n > 0) ? 1 : -1);
  262.     while (n) {
  263.         curwp->w_doto = 0;    /* start at the beginning */
  264.  
  265.         /* detab the entire current line */
  266.         while (curwp->w_doto < llength(curwp->w_dotp)) {
  267.             /* if we have a tab */
  268.             if (lgetc(curwp->w_dotp, curwp->w_doto) == '\t') {
  269.                 ldelete(1L, FALSE);
  270.                 insspace(TRUE,
  271.                     tabsize - (curwp->w_doto % tabsize));
  272.             }
  273.             forwchar(FALSE, 1);
  274.         }
  275.  
  276.         /* advance/or back to the next line */
  277.         forwline(TRUE, inc);
  278.         n -= inc;
  279.     }
  280.     curwp->w_doto = 0;    /* to the begining of the line */
  281.     thisflag &= ~CFCPCN;    /* flag that this resets the goal column */
  282.     lchange(WFEDIT);    /* yes, we have made at least an edit */
  283.     retur